package picture

import (
	"chain_bridge_service/internal/pkg/core"
	"chain_bridge_service/internal/repository/mongodb/picture"
	"encoding/hex"
	"math/rand"
	"mime/multipart"
	"os"
	"path"
	"path/filepath"
	"strings"
)

type PicturesData struct {
	Id         string
	PictureUrl string
	SkipUrl    string
	FileName   string
}

type CreateData struct {
	Id      string
	File    *multipart.FileHeader
	SkipUrl string
}

func (s *service) Create(ctx core.Context, createData *PicturesData) error {
	pictureModel := picture.NewModel()
	pictureModel.PictureUrl = createData.PictureUrl
	pictureModel.SkipUrl = createData.SkipUrl
	pictureModel.FileName = createData.FileName
	err := pictureModel.Create(s.db.GetDb())
	return err
}

func (s *service) Update(ctx core.Context, createData *PicturesData) error {
	pictureModel := picture.NewModel()
	pictureModel.Id = createData.Id
	oldData, err := pictureModel.FindOne(s.db.GetDb())
	if err != nil {
		return err
	}
	fullFilePath := filepath.Join("./static/uploadfile", oldData.FileName)
	err = os.Remove(fullFilePath)
	if err != nil {
		return err
	}

	pictureModel.PictureUrl = createData.PictureUrl
	pictureModel.SkipUrl = createData.SkipUrl
	pictureModel.FileName = createData.FileName
	err = pictureModel.Update(s.db.GetDb())
	return err
}

func (s *service) List(ctx core.Context, pageIndex uint, pageSize uint) ([]picture.Picture, error) {
	pictureModel := picture.NewModel()
	list, err := pictureModel.List(s.db.GetDb(), pageIndex, pageSize)
	return list, err
}

func (s *service) Delete(ctx core.Context, pictureData *PicturesData) error {
	pictureModel := picture.NewModel()
	fullFilePath := filepath.Join("./static/uploadfile", pictureData.FileName)
	err := os.Remove(fullFilePath)
	if err != nil {
		return err
	}
	pictureModel.Id = pictureData.Id
	err = pictureModel.Delete(s.db.GetDb())
	return err
}
func (s *service) Count(ctx core.Context) (int64, error) {
	pictureModel := picture.NewModel()
	count, err := pictureModel.Count(s.db.GetDb())
	return count, err
}

func Ext(path string) string {
	for i := len(path) - 1; i >= 0 && path[i] != '/'; i-- {
		if path[i] == '.' {
			return path[i:]
		}
	}
	return ""
}

func Upload(ctx core.Context, file *multipart.FileHeader) (string, string, error) {
	scheme := "http://"
	if ctx.TLS() != nil {
		scheme = "https://"
	}
	host := scheme + ctx.Request().Host
	filepath := path.Join("./static/uploadfile")
	randomName, err := generateRandomFileName(file.Filename)
	if err != nil {
		return "", "", err
	}
	//上传到的路径
	pathRelative := path.Join(filepath, randomName) //路径+文件名上传
	//如果没有filepath文件目录就创建一个
	if _, err := os.Stat(filepath); err != nil {
		if !os.IsExist(err) {
			err := os.MkdirAll(filepath, os.ModePerm)
			return "", "", err
		}
	}

	err = ctx.SaveFile(file, pathRelative)
	if err != nil {
		return "", "", err
	}

	return host + "/" + pathRelative, randomName, nil

}

func generateRandomFileName(extension string) (string, error) {
	// 生成随机字节
	bytes := make([]byte, 16)
	if _, err := rand.Read(bytes); err != nil {
		return "", err
	}

	// 将字节转换为十六进制字符串
	randomString := hex.EncodeToString(bytes)

	// 返回带扩展名的随机文件名
	return randomString + strings.ToLower(extension), nil
}
